//
//  DECXDeliveryListViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/9/10.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXDeliveryListViewController.h"
#import "DECXDeliveryTableCell.h"
#import "DECXManager.h"
#import "DECXDeliveryOrder.h"
#import "UIImageView+WebCache.h"
#import "DECXDeliveryPayTableCell.h"
#import "DECXWarehouseChargeManager.h"
#import "TPWalletInfoViewController.h"

@interface DECXDeliveryListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *orderTable;
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) DECXWarehouseChargeManager *wManager;
@property (strong, nonatomic) NSArray *dataAry;
@end

@implementation DECXDeliveryListViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self fetchDeliveryList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DECXManager new];
    self.wManager = [DECXWarehouseChargeManager new];
}
- (void)fetchDeliveryList
{
    MyUser;
    weak_Self(self);
    NSString *type = @"";
    if ([DECXGlobal shareInstance].loginType.integerValue == 2) {
        type = @"2";
    }else{
        type = @"1";
    }
    [[self.manager deliveryList:user.userid type:type]subscribeNext:^(id x) {
        weakSelf.dataAry = x;
        [weakSelf.orderTable reloadData];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DECXDeliveryOrder *order = self.dataAry[indexPath.row];
    if (order.order.integerValue == 1) {
        return 210.0;
    }else{
        return 150.0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DECXDeliveryOrder *order = self.dataAry[indexPath.row];
    if (order.order.integerValue == 1) {
        //线上支付
        static NSString *indentifier = @"DeliveryPayCell";
        DECXDeliveryPayTableCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        if (!cell) {
            cell = [[DECXDeliveryPayTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delNoLbl.text = [NSString stringWithFormat:@"提货码:%@",order.get_id];
        cell.delOderNoLbl.text = [NSString stringWithFormat:@"提单号:%@",order.getlist_id];
        cell.delTimeLbl.text = [NSString stringWithFormat:@"申请时间:%@",order.get_atime];
        cell.delTimeLbl.text = [NSString stringWithFormat:@"申请时间:%@",order.get_atime];
        if ([order.order_state isEqualToString:@"notpay"]) {
            cell.delStatusLbl.text = @"等待支付";
            cell.payBtn.hidden = NO;
            cell.cancelBtn.hidden = NO;
        }else if ([order.order_state isEqualToString:@"reject"]){
            cell.delStatusLbl.text = @"已取消";
            cell.payBtn.hidden = YES;
            cell.cancelBtn.hidden = YES;
        }else{
            if ([order.isget isEqualToString:@"1"]) {
                cell.delStatusLbl.text = @"等待提货";
            }else if ([order.isget isEqualToString:@"0"]){
                cell.delStatusLbl.text = @"不允许提货";
            }else if ([order.isget isEqualToString:@"2"]){
                cell.delStatusLbl.text = @"提货完成";
            }
            cell.payBtn.hidden = YES;
            cell.cancelBtn.hidden = YES;
        }
        [cell.productLogo sd_setImageWithURL:[NSURL URLWithString:order.getlist_pic_small] placeholderImage:nil];
        cell.nameLbl.text = [NSString stringWithFormat:@"%@(×%@吨)",order.goods_type,order.get_weight];
        cell.remainLbl.text = [NSString stringWithFormat:@"剩余数量:%@吨",order.get_weight_remain];
        cell.cangchuMoneyLbl.text = [NSString stringWithFormat:@"仓储费:￥%@",order.depot_price];
        [cell payOrderHandler:^{
            [self payOnLine:order.order_id];
        }];
        [cell cancelOrderHandler:^{
            [self cancelOrder:order.xid success:^{
                order.order_state = @"reject";
                [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }];
        }];
        return cell;
    }else{
        static NSString *indentifier = @"DeliveryCell";
        DECXDeliveryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        if (!cell) {
            cell = [[DECXDeliveryTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delNoLbl.text = [NSString stringWithFormat:@"提货码:%@",order.get_id];
        cell.delOderNoLbl.text = [NSString stringWithFormat:@"提单号:%@",order.getlist_id];
        cell.delTimeLbl.text = [NSString stringWithFormat:@"申请时间:%@",order.get_atime];
        cell.delTimeLbl.text = [NSString stringWithFormat:@"申请时间:%@",order.get_atime];
        if ([order.isget isEqualToString:@"1"]) {
            cell.delStatusLbl.text = @"等待提货";
        }else if ([order.isget isEqualToString:@"0"]){
            cell.delStatusLbl.text = @"不允许提货";
        }else if ([order.isget isEqualToString:@"2"]){
            cell.delStatusLbl.text = @"提货完成";
        }
        [cell.productLogo sd_setImageWithURL:[NSURL URLWithString:order.getlist_pic_small] placeholderImage:nil];
        cell.nameLbl.text = [NSString stringWithFormat:@"%@(×%@吨)",order.goods_type,order.get_weight];
        cell.remainLbl.text = [NSString stringWithFormat:@"剩余数量:%@吨",order.get_weight_remain];
        return cell;
    }
}
#pragma mark --- 线上支付
- (void)payOnLine:(NSString *)oid
{
    MyUser;
    weak_Self(self);
    [self showHUD];
    [[self.wManager payOrderWithOid:oid uid:user.userid]subscribeNext:^(id x) {
        NSString *url = x[@"obj"];
        TPWalletInfoViewController *vc = [[TPWalletInfoViewController alloc]init];
        vc.walletUrl = url;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        [weakSelf hideHUD];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:1.0];
    } completed:^{
        
    }];
}
- (void)cancelOrder:(NSString *)oid success:(void(^)(void))block
{
    weak_Self(self);
    [self showMessageHUD:@"正在取消订单..."];
    [[self.wManager cancelDeliveryOrder:oid]subscribeNext:^(id x) {
        [weakSelf showMessageHUD:@"取消成功" delayHide:1.0];
        if (block) {
            block();
        }
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:1.0];
    } completed:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
